﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AvalonDock.Layout.Serialization;
using System.IO;
using AvalonDock.Layout;
using System.Diagnostics;
using Draco.OWF.Process;
using Draco.OWF.Acticity;
using Draco.OWF.Transition;
using Draco.OWF.Acticity.General;
using Draco.OWF.Studio.Designer;
using Draco.OWF.Studio.Common.Lang;

namespace Draco.OWF.Studio
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public void AddLaoutDocumentWindow(LayoutDocument layoutDocument)
        {
            this.MainContentArea.Children.Add(layoutDocument);
        }
        public LayoutDocument ActivedContent
        {
            get 
            {
                return this.MainContentArea.SelectedContent as LayoutDocument;
            }
        }

        #region layout
        private void OnLayoutRootPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            var activeContent = ((LayoutRoot)sender).ActiveContent;
            if (e.PropertyName == "ActiveContent" && activeContent != null)
            {
                Debug.WriteLine(string.Format("ActiveContent-> {0}", activeContent));
            }
        }
        private void OnLoadLayout(object sender, RoutedEventArgs e)
        {
            string fileName = (sender as MenuItem).Header.ToString();
            var serializer = new XmlLayoutSerializer(dockManager);
            serializer.LayoutSerializationCallback += (s, args) =>
            {
                //if (args.Model.ContentId == "winFormsHost")
                //    args.Content = winFormsHost;
            };
            using (var stream = new StreamReader(string.Format(@".\AvalonDock_{0}.config", fileName)))
                serializer.Deserialize(stream);
        }

        private void OnSaveLayout(object sender, RoutedEventArgs e)
        {
            string fileName = (sender as MenuItem).Header.ToString();
            var serializer = new XmlLayoutSerializer(dockManager);
            using (var stream = new StreamWriter(string.Format(@".\AvalonDock_{0}.config", fileName)))
                serializer.Serialize(stream);
        }
        #endregion

        private void BtnLoadProcess_Click(object sender, RoutedEventArgs e)
        {
            IActivity ac1 = new EmptyActivity();
            InvokerActivity ac2 = new InvokerActivity();
            IRoute route = new Route(ac1.Id, ac2.Id);

            IActivity ac3 = new EmptyActivity();

            #region
            ac2.InvokObjectType = "Draco.OWF.Demo.DemoAssembly,Draco.OWF.Demo";
            ac2.InvokObjectRef = "";
            ac2.InvokeMethod = "HelloWord";
            DataBinding.BindingDataItem item = new DataBinding.BindingDataItem();
            item.DataName = "name";
            item.DataType = typeof(System.String);
            ac2.InputParameters.Add(item);

            DataBinding.BindingDataItem oitem = new DataBinding.BindingDataItem();
            oitem.DataName = "result";
            oitem.DataType = typeof(System.String);
            ac2.OutputParameters.Add(oitem);

            ac2.Render.X = 200;
            ac2.Render.Y = 300;

            ac3.Render.X = 400;
            ac3.Render.Y = 200;
            #endregion

            ProcessDef workflow = new ProcessDef();
            workflow.AddActivity(ac1);
            workflow.AddActivity(ac2);
            workflow.AddActivity(ac3);

            workflow.AddRoute(route);

            workflow.EndActivity = ac2;
            workflow.EnsureStartActivity();

            workflow.Check();

            LayoutDesigerWin win = new LayoutDesigerWin();
            win.LoadProcess(workflow);
            this.AddLaoutDocumentWindow(win);
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            MenuItem menu = sender as MenuItem;
            String CMD = menu.Tag as String;
            if (String.Compare(CMD, "Language_English", true) == 0)
            {
                menu.IsChecked = true;
                ChangeLanguage(SupportedLang.English);
            }
            else if (String.Compare(CMD, "Language_Chinese", true) == 0)
            {
                menu.IsChecked = true;
                ChangeLanguage(SupportedLang.Chinese);
            }
        }
        private void ToolBarButton_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            String CMD = btn.Tag as String;
            if (String.Compare(CMD, "CMD_WF_DESIGN_ACTION_Pointer", true) == 0)
            {
                if (ActivedContent is LayoutDesigerWin)
                {
                    LayoutDesigerWin dw = ActivedContent as LayoutDesigerWin;
                    dw.DesignerWin.DesignerPanel.DesignerEnv.ActivityFacetDragAction = MoveAction.MoveActivityFacet;
                }
            }
            else if (String.Compare(CMD, "CMD_WF_DESIGN_ACTION_DRAWLINE", true) == 0)
            {
                if (ActivedContent is LayoutDesigerWin)
                {
                    LayoutDesigerWin dw = ActivedContent as LayoutDesigerWin;
                    dw.DesignerWin.DesignerPanel.DesignerEnv.ActivityFacetDragAction = MoveAction.DrawRouteFacet;
                }
            }
        }

        private void ChangeLanguage(SupportedLang lang)
        {
            String languagefile="";
            switch (lang) 
            {
                case SupportedLang.English: languagefile = @"lang\en-US.xaml"; break;
                case SupportedLang.Chinese: languagefile = @"lang\zh-CN.xaml"; break;
                default: languagefile = @"lang\en-US.xaml"; break;
            }
            Application.Current.Resources.MergedDictionaries[0] = new ResourceDictionary()
            {
                Source = new Uri(languagefile, UriKind.RelativeOrAbsolute)
            };
        }

        
    }
}
